package com.etoc.test.flow.zs;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.etoc.test.util.DateUtil;
import com.etoc.test.util.EncryptUtil;
import com.etoc.test.util.HttpUtil;

/**
 * 
 * �û������ʸ��ѯʵ����
 * <������ϸ����>
 * 
 * @author  �³�
 * @version  [�汾��, 2015-8-10]
 * @see  [�����/����]
 * @since  [��Ʒ/ģ��汾]
 */
public class CheckOrder implements Serializable {
	/**
	 * ע������
	 */
	private static final long serialVersionUID = -6769857246531026627L;
	private String action;
	private String appKey;
	private String appSecret;
	private String pkgNo;
	private String phoneNo;
	private String timeStamp;
	private String transNo;
	private String sign;
	Map<String, String> map = new TreeMap<String, String>();
	
	public CheckOrder() {
		super();
	}

	public CheckOrder(String action, String appKey, String appSecret, String pkgNo, String phoneNo, String transNo, String timeStamp, String sign) {
		super();
		this.action = action;
		this.appKey = appKey;
		this.appSecret = appSecret;
		this.pkgNo = pkgNo;
		this.phoneNo = phoneNo;
		this.transNo = transNo;
		this.timeStamp = timeStamp;
		this.sign = sign;
	}
	
	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}
	
	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getPkgNo() {
		return pkgNo;
	}

	public void setPkgNo(String pkgNo) {
		this.pkgNo = pkgNo;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getTransNo() {
		return transNo;
	}

	public void setTransNo(String transNo) {
		this.transNo = transNo;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
	
	public Map<String, String> getMap() {
		return map;
	}

	public String signString() {
		if (this.action != null && !this.action.equals(""))
		{
			map.put("action", this.action);
		}
		if (this.appKey != null && !this.appKey.equals(""))
		{
			map.put("appKey", this.appKey);
		}
		if (this.pkgNo != null && !this.pkgNo.equals(""))
		{
			map.put("pkgNo", this.pkgNo);
		}
		if (this.phoneNo != null && !this.phoneNo.equals(""))
		{
			map.put("phoneNo", this.phoneNo);
		}
		if (this.transNo != null && !this.transNo.equals(""))
		{
			map.put("transNo", this.transNo);
		}		
		if (this.timeStamp != null && !this.timeStamp.equals(""))
		{
			map.put("timeStamp", timeStamp);
		}
		
		Iterator<String> keySet = map.keySet().iterator();
		StringBuffer signString = new StringBuffer(appSecret);
		while(keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			signString.append(key).append(val);
		}
		signString.append(appSecret);
		
		return signString.toString();
	}

	@Override
	public String toString() {
		Iterator<String> keySet = map.keySet().iterator();
		String ret = "";
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			ret += key + "=" + val + "&";
		}
		ret += "sign" + "=" + sign;
		return ret;
	}

	public static void main(String[] args)
	{
		CheckOrder order = new CheckOrder();
		order.setAction("checkOrder");
		order.setAppKey("fee99ad5e5074aba8ae1479bcb14c612");
		order.setAppSecret("1");
		order.setPkgNo("002000");
		order.setPhoneNo("18671326122");
		order.setTransNo(DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss") + "_" + order.getPhoneNo());
		order.setTimeStamp(DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss"));
		String sign = order.signString();
		order.setSign(EncryptUtil.encryptSHA(sign));
		
		String url = "http://61.50.245.231:91/flowAgent";
		System.out.println(url + "?" + order.toString());
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Map<String, String> map = order.getMap();
		Iterator<String> keySet = map.keySet().iterator();
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			nvps.add(new BasicNameValuePair(key, val));
		}
		nvps.add(new BasicNameValuePair("sign", order.getSign()));
		
		long startTime = System.currentTimeMillis();
		String respCode = HttpUtil.httpRequest(url, nvps);
		long endTime = System.currentTimeMillis();
		System.out.println("checkOrder=" + respCode);
		System.out.println("spend " + (endTime - startTime) + " milli second.");
	}
	
}
